import java.util.Enumeration;
import java.util.Random;
import java.util.Scanner;
import java.io.*;

public class TeamDriver {




    public static void main(String args[])
    {
    	
    	Team[] NFL=new Team[32];
    	
    	
    	for(int i=0;i<32;i++)
    		NFL[i]=new Team();
    	
    	
    	Scanner sc;
    	File data=new File("stats.txt");
    	int teamIndex=0;
    	try
    	{
    		sc = new Scanner(data);
    		
	    	while (sc.hasNextLine()) 
	      	{
	         
	         
	         String line=sc.nextLine();
	         
	         int index=0;
	         
	         
	        index=line.indexOf('\t');  //updates the 	         
	        NFL[teamIndex].teamName=line.substring(0,index);                     //TEAMNAME
	        line=line.substring(index+1);  //cuts out the first thing 	       
	         
			index=line.indexOf('\t');  //updates the index
			NFL[teamIndex].div=line.substring(0,index);                          //DIVISION
			line=line.substring(index+1);  //cuts out the first thing	        
	        
	        index=line.indexOf('\t');  //updates the index
			NFL[teamIndex].conf=line.substring(0,index);                          //CONFERENCE
	        line=line.substring(index+1);  //cuts out the first thing 
			
			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].W=Double.parseDouble(line.substring(0,index));				//WINS
			NFL[teamIndex].insertStat("W", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing 
	        
	        index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].L=Double.parseDouble(line.substring(0,index));				//Loses
			NFL[teamIndex].insertStat("L", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing 
	        
	        index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].T=Double.parseDouble(line.substring(0,index));				//ties
			NFL[teamIndex].insertStat("T", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing 
	         
	        index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].PCT=Double.parseDouble(line.substring(0,index));				//PCT
			NFL[teamIndex].insertStat("PCT", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing 
			
			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].PF=Double.parseDouble(line.substring(0,index));				//PF
			NFL[teamIndex].insertStat("PF", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing 
			
			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].PA=Double.parseDouble(line.substring(0,index));				//PA
			NFL[teamIndex].insertStat("PA", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing 
			
			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].NetPts=Double.parseDouble(line.substring(0,index));			//NetPts
			NFL[teamIndex].insertStat("NetPts", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing 
			
			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].TD=Double.parseDouble(line.substring(0,index));				//Touchdown
			NFL[teamIndex].insertStat("TD", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing 
			
			index=line.indexOf('-');  //updates the index
			//NFL[teamIndex].HW=Double.parseDouble(line.substring(0,index));				//home wins
			NFL[teamIndex].insertStat("HW", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].HL=Double.parseDouble(line.substring(0,index));				//home Loses
			NFL[teamIndex].insertStat("HL", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('-');  //updates the index
			//NFL[teamIndex].RW=Double.parseDouble(line.substring(0,index));				//road wins
			NFL[teamIndex].insertStat("RW", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].RL=Double.parseDouble(line.substring(0,index));				//road Loses
			NFL[teamIndex].insertStat("RL", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('-');  //updates the index
			//NFL[teamIndex].DW=Double.parseDouble(line.substring(0,index));				//division wins
			NFL[teamIndex].insertStat("DW", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].DL=Double.parseDouble(line.substring(0,index));				//division Loses
			NFL[teamIndex].insertStat("DL", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing

			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].divPCT=Double.parseDouble(line.substring(0,index));			//division percentage
			NFL[teamIndex].insertStat("divPCT", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('-');  //updates the index
			//NFL[teamIndex].CW=Double.parseDouble(line.substring(0,index));				//Conference wins
			NFL[teamIndex].insertStat("CW", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].CL=Double.parseDouble(line.substring(0,index));				//Conference Loses
			NFL[teamIndex].insertStat("CL", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].confPCT=Double.parseDouble(line.substring(0,index));			//conference percentage
			NFL[teamIndex].insertStat("confPCT", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('-');  //updates the index
			//NFL[teamIndex].NCW=Double.parseDouble(line.substring(0,index));				//nonConference wins
			NFL[teamIndex].insertStat("NCW", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('\t');  //updates the index
			//NFL[teamIndex].NCL=Double.parseDouble(line.substring(0,index));				//nonConference Loses
			NFL[teamIndex].insertStat("NCL", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('\t');  //updates the 
			NFL[teamIndex].streak(line.substring(0,index));								//Streak
			//NFL[teamIndex].insertStat("streak", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing			
			
			index=line.indexOf('-');  //updates the index
			//NFL[teamIndex].lastW=Double.parseDouble(line.substring(0,index));				//last 5 wins
			NFL[teamIndex].insertStat("lastW", Double.parseDouble(line.substring(0,index)));
			line=line.substring(index+1);  //cuts out the first thing
			
			index=line.indexOf('Q');  //updates the 
			//NFL[teamIndex].lastL=Double.parseDouble(line.substring(0,index));				//last 5 loses
			NFL[teamIndex].insertStat("lastL", Double.parseDouble(line.substring(0,index)));
			//line=line.substring(index+1);  //cuts out the first thing
			
	        teamIndex++;
	      }
	      	
	      	
    	}
    	catch (Exception e) {
    		System.out.println(e.toString());
    	}
      
      System.out.println("\n");
      
      if(NFL[4].statIndex.containsKey("PCT"))
    	  System.out.println( NFL[4].statIndex.get("PCT"));
      else
    	  System.out.println("Didn't Exist");
      
      for(int i = 0; i < 32; i ++)
    	  System.out.println(NFL[i] + "\n");
      
      String randStat = NFL[31].getRandomStat();
      System.out.println("\n\nA random stat is " + randStat + " " + NFL[31].getStat(randStat));
      System.out.println("Creating Tree");
      
      EquationTree eq = new EquationTree(3, NFL[31]);
      
      eq.displayPreorder();
      System.out.println();
      eq.displayInorder();
      System.out.println();
      
      for(int i = 0; i < NFL.length; i ++)
    	  System.out.printf("%25s %s\n",NFL[i].teamName, eq.compute(NFL[i]));
    }
    
    public static String getRandomStat(Team team){
    	Enumeration<String> teamStats = team.getListOfStats();
    	Random rand = new Random();
    	String stat = "";
    	int randInt = rand.nextInt(team.statIndex.size());
    	int counted = 0;
    	
    	while(teamStats.hasMoreElements() && counted ++ <= randInt){
    		stat = teamStats.nextElement();
    	}
    	
    	return stat;
    }
}